#include "swupdateclient.h"

#include <QTimer>
#include <QDebug>
#include <QLocale>
#include <QThread>
SwUpdateClient::SwUpdateClient(QObject *parent)
    : QObject(parent)
{
    m_progressSocket = new QLocalSocket(this);


    connect(m_progressSocket, &QLocalSocket::readyRead, this, &SwUpdateClient::onProgressReadyRead);
    connect(m_progressSocket, &QLocalSocket::errorOccurred, this, &SwUpdateClient::onProgressError);
    connect(m_progressSocket, &QLocalSocket::stateChanged, this, &SwUpdateClient::onProgressStateChanged);

    connect(&this->swUpdateFileWatch,&QFileSystemWatcher::directoryChanged,this,&SwUpdateClient::directoryChanged);
    // this->swUpdateFileWatch.addPath(m_progressSocketPath);
    this->swUpdateFileWatch.addPath("/tmp/");
}

SwUpdateClient::~SwUpdateClient()
{

    stop();
}

void SwUpdateClient::start()
{
    qDebug() << "start-------------------------------";
    tryConnectProgress();
}

void SwUpdateClient::stop()
{
    if (m_progressSocket->state() == QLocalSocket::ConnectedState){
        qDebug () << "disconnectFromServer==============================";
        m_progressSocket->disconnectFromServer();
    }
}



void SwUpdateClient::tryConnectProgress()
{


    QFileInfo file(m_progressSocketPath);
    do{
        file.refresh();
        if(file.exists()){
            m_progressSocket->connectToServer(m_progressSocketPath);

            if (m_progressSocket->waitForConnected(3000)) {
                // qDebug() << "[Progress] Connect Success";
                break;
            }
        }else{
            break;
        }

        QThread::msleep(10);


    }while(file.exists() && m_progressSocket->state() == QLocalSocket::UnconnectedState );

}



void SwUpdateClient::onProgressReadyRead()
{
    QByteArray data = m_progressSocket->readAll();
    if (!data.isEmpty()) {
        emit progressMessageReceived(data);
    }
}


void SwUpdateClient::onProgressError(QLocalSocket::LocalSocketError error)
{
    Q_UNUSED(error)
    // qDebug() << "[Progress] Error" << m_progressSocket->errorString();
}



void SwUpdateClient::onProgressStateChanged(QLocalSocket::LocalSocketState state)
{
    switch (state) {
    case QLocalSocket::UnconnectedState:
        qDebug() << "[Progress] UnconnectedState";
        // emit progressDisconnected();
        // m_progressRetryTimer->start(m_retryInterval);
        break;
    case QLocalSocket::ConnectingState:
        qDebug() << "[Progress] ConnectingState...";
        break;
    case QLocalSocket::ConnectedState:
        qDebug() << "[Progress] ConnectedState";
        // m_progressRetryTimer->stop();
        // emit progressConnected();
        break;
    default:
        break;
    }
}


void SwUpdateClient::directoryChanged(const QString &path)
{
    QFileInfo file(m_progressSocketPath);
    if(file.exists() && this->isOpen == false){
        this->isOpen = true;
        qDebug() << "start===================";
        this->start();
    }else if(this->isOpen == true && !file.exists()){
        this->isOpen = false;
        this->stop();
    }
}
